package com.hundsun.fcp.activity;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

import com.hundsun.fcp.R;
import com.hundsun.fcp.base.BaseActivity;
import com.hundsun.fcp.common.AppointFiled.RequestCodeDef;
import com.hundsun.fcp.common.Redirect;
import com.hundsun.fcp.dao.MineDao;
import com.hundsun.fcp.manager.FcpUserManager;
import com.hundsun.fcp.manager.SettingManager.FcpUserSetting;
import com.hundsun.fcp.widget.lockpattern.LockPatternUtils;

public class MineAccountSecurityActivity extends BaseActivity implements OnClickListener{
	//修改登录密码
	private View mModifyLoginPwd;
	//修改交易密码
	private View mModifyTransPwd;
	//找回交易密码
	private View mResetTransPwd;
	//是否开启手势密码
	private ImageView mSwitchGesture;
	//修改手势密码
	private View mModifyGesturePwd;
	//退出登录
	private Button mQuitLogin;
	
	private LockPatternUtils mLockPatternUtils;
	@Override
	protected int getContentLayout() {
		return R.layout.fcp_mine_account_safe;
	}
	protected void initHeader() {
		super.initHeader();
		mHeaderMenu.setTitle(R.string.mine_account_security);
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		if (FcpUserManager.getInstance().hasLoginUser()) {
			if (FcpUserSetting.hasLockPattern()) {
				switchOn();
			} else {
				switchOff();
			}

		} else {
			finish();
		}
	}
	private void switchOn() {
		mSwitchGesture.setImageResource(R.drawable.switch_on) ;
		mModifyGesturePwd.setVisibility(View.VISIBLE);
	}
	private void switchOff() {
		mSwitchGesture.setImageResource(R.drawable.switch_off) ;
		mModifyGesturePwd.setVisibility(View.GONE);
	}
	
	@Override
	public void initView() {
		super.initView();
		mLockPatternUtils = new LockPatternUtils(this) ;
		mModifyLoginPwd = findViewById(R.id.rl_modify_pwd);
		mModifyTransPwd = findViewById(R.id.rl_modify_trans_pwd);
		mResetTransPwd = findViewById(R.id.rl_reset_trans_pwd);
		mModifyGesturePwd = findViewById(R.id.rl_modify_gestures_pwd);
		mSwitchGesture = (ImageView) findViewById(R.id.iv_switch_gesture);
		mQuitLogin = (Button) findViewById(R.id.mine_quit);
		
		mModifyLoginPwd.setOnClickListener(this);
		mModifyTransPwd.setOnClickListener(this);
		mResetTransPwd.setOnClickListener(this);
		mModifyGesturePwd.setOnClickListener(this);
		mQuitLogin.setOnClickListener(this);
		mSwitchGesture.setOnClickListener(this);
	}
	@Override
	public void onClick(View v) {
		if(v == mModifyLoginPwd){
			Redirect.startModifyLoginPwd(this);
		}else if(v == mModifyTransPwd) {
			Redirect.startModifyTransPwd(this);
		}else if(v == mResetTransPwd){
			Redirect.startResetTransPwd(this);
		}else if(v == mModifyGesturePwd){
			Redirect.startModifyGesturePwd(this,RequestCodeDef.PARTTERN_REQUEST_CODE_MODIFY);
		}else if(v == mSwitchGesture){
			if (!FcpUserSetting.hasLockPattern()) {
				Redirect.startCreateLockPattern(MineAccountSecurityActivity.this);
			}else{
				Redirect.startModifyGesturePwd(this,RequestCodeDef.PARTTERN_REQUEST_CODE_FORGET);
			}
		}else if(v == mQuitLogin){
			AlertDialog.Builder builder = new Builder(this)
			.setMessage("你确定要退出当前账户？")
			.setNegativeButton("取消", null)
			.setPositiveButton("确定", new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					FcpUserManager.getInstance().cleanStoredUserInfo();
					new MineDao(MineAccountSecurityActivity.this).clearMineResult() ;
					finish() ;
				}
			}) ;
			builder.create().show() ;
		}
	}
	
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (resultCode == RESULT_OK) {
			if (requestCode == RequestCodeDef.PARTTERN_REQUEST_CODE_FORGET) {
				mLockPatternUtils.clearLock();
				switchOff();
			} else if (requestCode == RequestCodeDef.PARTTERN_REQUEST_CODE_MODIFY) {
				Redirect.startCreateLockPattern(this);
			} 
		}

	}

}
